import { sessionStorage } from "../assets/js/storage";
import router from "../router/index";
const mutations = {
  user_id(state, user) {
    state.user = user;
    sessionStorage.setItem("user_id", user);
  },
  token(state, token) {
    state.token = token;
    sessionStorage.setItem("token", token);
  },
  salesAuthorityId(state, salesAuthorityId) {
    state.salesAuthorityId = salesAuthorityId;
    sessionStorage.setItem("salesAuthorityId", salesAuthorityId);
  },
  setToolData(state, data) {
    // 添加标签按钮，如果当前路由已经打开，则不再重复添加
    if (router.app.$route.meta.keepAlive) {
      const inToolbar = state.toolBarData.find(
        (item) => item.detail === data.detail
      );
      if (data.name != "物流首页" && data.name != "首页")
        !inToolbar && state.toolBarData.push({ ...data });
    }
    // console.log(router.app.$route.meta.keepAlive)
  },
  addOrderCount(state, count) {
    state.orderCount = count;
  },
  setCacheView(state, data) {
    // 与setToolData类似
    if (state.cacheView.includes(data.componentName)) return;
    if (data.name != "物流首页" && data.name != "首页")
      state.cacheView.push(data.componentName);
  },
  clearToolItem(state, detail) {
    const index = state.toolBarData.findIndex((item) => item.detail === detail);
    const isActive =
      router.app.$route.path == state.toolBarData[index]["detail"];
    const len = state.toolBarData.length - 1;
    state.toolBarData.splice(index, 1);
    // (index == len || isActive) && router.push({path:state.toolBarData[state.toolBarData.length - 1]["detail"]});
  },
  clearCacheView(state, viewName) {
    const index = state.cacheView.findIndex((item) => item == viewName);
    state.cacheView.splice(index, 1);
  },
  closeTar(state, e) {
    state.toolBarData = [];
    state.cacheView = [];
  },
  updateTabStorage(state, e) {
    state.tabStorage = e;
  },
};

export default mutations;
